package servlet;

import com.alibaba.fastjson.JSON;
import entity.Financingproduct;
import service.Impl.financingproductServlceImpl;
import service.financingprocuctService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.List;

@WebServlet(urlPatterns = "/financingproduct")
public class financingproductServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out =resp.getWriter();
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String contextpath=req.getContextPath();
        financingprocuctService financingprocuctService=new financingproductServlceImpl();
        String opr=req.getParameter("opr");
        int id1= Integer.parseInt(req.getParameter("id1"));
        String risk1= req.getParameter("risk1");
        if ("select".equals(opr)){
            List<Financingproduct> list=financingprocuctService.select(id1,risk1);
            String text=JSON.toJSONString(list);
            out.print(text);
        }else if ("add".equals(opr)){
            int id= Integer.parseInt(req.getParameter("id"));
            String risk=req.getParameter("risk");
            String income=req.getParameter("income");
            java.sql.Date saleStarting= java.sql.Date.valueOf(req.getParameter("saleStarting")) ;
            java.sql.Date saleEnd=java.sql.Date.valueOf(req.getParameter("saleEnd"));
            Date end=java.sql.Date.valueOf(req.getParameter("end"));
            Financingproduct financingproduct1=new Financingproduct();
            financingproduct1.setId(id);
            financingproduct1.setRisk(risk);
            financingproduct1.setIncome(income);
            financingproduct1.setSaleStarting(saleStarting);
            financingproduct1.setSaleEnd(saleEnd);
            financingproduct1.setEnd(end);
            int hanghsu=financingprocuctService.add(financingproduct1);
            if (hanghsu>0){
                out.print("<script>alert('添加成功!');location.href='"+contextpath+"/index.jsp'</script>");
            }else {
                out.print("<script>alert('添加失败!');location.href='"+contextpath+"/add.jsp'</script>");
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
